package com.magic.gre.ui.fragment;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.base.fragment.BaseMVPFragment;
import com.magic.gre.helper.Contract;
import com.magic.gre.helper.WXConfig;
import com.magic.gre.mvp.contract.AccountLoginContract;
import com.magic.gre.mvp.presenter.AccountLoginPresenterImpl;
import com.magic.gre.ui.activity.ForgetPwsCodedActivity;
import com.magic.gre.ui.activity.MainActivity;
import com.magic.gre.ui.activity.WebActivity;
import com.noname.lib_base_java.util.EncryptUtils;
import com.noname.lib_base_java.util.PathUtil;
import com.noname.lib_base_java.util.ToastUtil;
import com.noname.lib_base_java.util.ValidateUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 14:56
 * 文件名: AccountLoginFragment
 * 描述:
 */
public class AccountLoginFragment extends BaseMVPFragment<AccountLoginPresenterImpl> implements AccountLoginContract.View {

    @BindView(R.id.phone_edt)
    EditText phoneEdt;
    @BindView(R.id.pwsd_edt)
    EditText pwsdEdt;
    @BindView(R.id.forget_pwsd_tv)
    TextView forgetPwsdTv;
    @BindView(R.id.agreement_tv)
    TextView agreementTv;
    @BindView(R.id.agreement_tv2)
    TextView agreementTv2;

    public static AccountLoginFragment newInstance() {
        return new AccountLoginFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login_account;
    }

    @Override
    protected AccountLoginPresenterImpl creatPresenter() {
        return new AccountLoginPresenterImpl(this);
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void netOverride() {

    }

    @OnClick(value = {R.id.forget_pwsd_tv, R.id.login_tv, R.id.agreement_tv2, R.id.agreement_tv,R.id.wx_login})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_pwsd_tv:
                ForgetPwsCodedActivity.startThis(mContext);
                break;
            case R.id.login_tv:
                login();
                break;
            case R.id.agreement_tv2:
                WebActivity.startThis(mContext, "用户协议", PathUtil.urlPath(Contract.AGREEMENT_URL));
                break;
            case R.id.agreement_tv:
                agreementTv.setSelected(!agreementTv.isSelected());
                break;
            case R.id.wx_login:
                loginWx();
                break;
        }
    }

    /**
     * 微信登陆
     */
    private void loginWx() {
        IWXAPI api= WXAPIFactory.createWXAPI(mContext, WXConfig.APP_ID_WX, true);

        api.registerApp(WXConfig.APP_ID_WX);
        SendAuth.Req req=new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = WXConfig.APP_STATE_WX;
        api.sendReq(req);

    }

    private void login() {

        if (!agreementTv.isSelected()){
            ToastUtil.getInstance().showNormal(mContext, "您未同意《用户使用协议》");
            return;
        }

        String phoneStr = String.valueOf(phoneEdt.getText());
        if (!ValidateUtil.Mobile(phoneStr)) {
            ToastUtil.getInstance().showNormal(mContext, "请输入正确手机号");
            return;
        }

        String pwsdStr = String.valueOf(pwsdEdt.getText());
        if (pwsdEdt.length() < 6 || pwsdEdt.length() > 20) {
            ToastUtil.getInstance().showNormal(mContext, "密码不得低于6位并且不得高于20位");
            return;
        }

        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("phone", phoneStr);
        parmsMap.put("password", EncryptUtils.encodeByMd5(pwsdStr));
        mPresenter.pLogin(parmsMap);
    }


    @Override
    public void vLogin() {
        MainActivity.startThis(mContext);
    }
}
